#Region " Imports "

Imports BlazeApps.Library.Page

#End Region

Partial Class Usercontrols_Options_Rating
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Public Property ItemGuid() As Guid
        Get
            Return GetGuidFromVS("ItemGuid")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ItemGuid", value)
        End Set
    End Property

    Public Property Revision() As Integer
        Get
            Return GetIntegerFromVS("Revision")
        End Get
        Set(ByVal value As Integer)
            ViewState.Add("Revision", value)
        End Set
    End Property

    Public Property HideData() As Boolean
        Get
            Return GetBooleanFromVS("HideData")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("HideData", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If ItemGuid = Guid.Empty Then
            GetQueryStringValues()
        End If

        SetPageState()

        If Me.Page.IsPostBack = False Then

            If IsUserAuthenticated Then
                ToggelRankingSection(ItemGuid, Revision)
            End If

        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveRating(ByVal Rating As Integer)
        Dim Rank As Ranks = Ranks.NewRanks

        Rank.itemid = ItemGuid
        Rank.userid = CurrentUserID
        Rank.rank = Rating
        Rank.revision = Revision
        Rank.Save()

        ToggelRankingSection(ItemGuid, Revision)
    End Sub

#End Region

#Region " Load Data "

    Public Sub ToggelRankingSection(ByVal ItemID As Guid, ByVal ItemRevision As Integer)

        If Not HideData Then
            If IsUserAuthenticated Then
                If Not (ItemGuid = Guid.Empty) Then
                    Dim Rank As Ranks = Ranks.GetRanks(ItemID, CurrentUserID, ItemRevision)

                    If Rank.hasdata = True Then
                        Me.uxPageRating.CurrentRating = Rank.rank
                        Me.uxPageRating.Enabled = False
                        Me.uxUserRatingLabel.Text = String.Format("{0} {1}", Resources.Resource.UserRanking.ToString, Rank.rank)
                    End If
                End If
            End If

        End If

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxPageRating_Changed(ByVal sender As Object, ByVal e As AjaxControlToolkit.RatingEventArgs) Handles uxPageRating.Changed
        SaveRating(CType(e.Value, Integer))
    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        ItemGuid = GetGuidFromQueryString("id")
    End Sub

    Private Sub SetPageState()

        Me.uxLoggedInPanel.Visible = IsUserAuthenticated()
        Me.uxPleaseLoginPanel.Visible = Not IsUserAuthenticated()

    End Sub

#End Region

End Class
